package com.lenovo.flutter_with_bmap.util;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class WebSocketHelper {
    private WebSocketClient webSocketClient;
    private List<String> message = new ArrayList<String>();
    private List<ListenerHelper> listeners = new ArrayList<ListenerHelper>();
    private String addr;
    private Timer timer = new Timer();
    public void connect(String addr){
        this.addr = addr;
        webSocketClient = createWebSocket();
        webSocketClient.connect();
        keepSocketConnect();
    }
    public void sendMsg(String msg){
        if(webSocketClient.getReadyState().toString()!="OPEN"){
            message.add(msg);
        }else {
            webSocketClient.send(msg);
        }
    }
    public void close(){
        webSocketClient.close();
        timer.cancel();
        timer.purge();
    }
    public void addListener(ListenerHelper listenerHelper){
        listeners.add(listenerHelper);
    }
    public void removeListener(ListenerHelper listenerHelper){
        listeners.remove(listenerHelper);
    }
    private void keepSocketConnect(){
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if( ! webSocketClient.getReadyState().toString().equals("OPEN")){
                    Log.e("连接断开", "守护进程判定ws已关闭，尝试重连");
                    try{
                        webSocketClient.close();
                        webSocketClient = createWebSocket();
                        webSocketClient.connect();
                    }catch (Exception e){
                        Log.e("eeee",e.toString());
                    }
                }
            }
        },5000,10*1000);
    }
    private WebSocketClient createWebSocket(){
        return new WebSocketClient(URI.create(addr)) {
            @Override
            public void onOpen(ServerHandshake handshakedata) {
                Log.i("onOpen","WebSocket已连接============================");
                Log.i("onOpen", webSocketClient.getReadyState().toString());
                while (message.size()>0){
                    webSocketClient.send(message.get(0));
                    message.remove(0);
                }
            }

            @Override
            public void onMessage(String message) {
                for(ListenerHelper l:listeners){
                    l.callBack(message);
                }
//                Log.i("onMessage","WebSocket收到消息============================"+message);
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                Log.i("onClose","WebSocket关闭===========================");
            }

            @Override
            public void onError(Exception ex) {
                Log.i("WebSocket发生错误",ex.toString());
            }
        };
    }
}

